/**
 * 
 */
package com.demo2do.boyle.backend.web.controller.system;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.system.TagService;
import com.demo2do.boyle.common.entity.system.tag.Tag;
import com.demo2do.boyle.common.entity.system.tag.Type;
import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.utils.StringUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;

/**
 * @author Leo
 *
 */
@Controller
@RequestMapping("/tags")
@MenuSetting("menu-system")
public class TagController {
	
	@Autowired
	private TagService tagService;
	
	/**
	 * list all tags
	 * 
	 */
	@RequestMapping("")
    @MenuSetting("submenu-system-tag-list")
    public ModelAndView list(Page page) {
    	
        ModelAndView modelAndView = new ModelAndView("system/tag/tag-list");
        
        List<Tag> tags = tagService.list(page);
        int count = tagService.count();
        
        modelAndView.addObject("tags", tags);
        modelAndView.addObject("count", count);
        
        return modelAndView;
    }
	
	/**
     * display the search panel
     * 
     * @return
     */
    @RequestMapping("/search")
    public String search() {
        return "system/tag/tag-search";
    }
    
    /**
     * do search
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-system-tag-list")
    public ModelAndView onSearch(Page page, HttpServletRequest request,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "type", required = false) Type type,
            @RequestParam(value = "createTimeBegin", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTimeBegin,
            @RequestParam(value = "createTimeEnd", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTimeEnd ) {

        ModelAndView modelAndView = new ModelAndView("system/tag/tag-list");

        Filter filter = new Filter().addLike("name", name)
                                    .addEquals("type", type)
                                    .addDateTimeBetween("createTime", createTimeBegin, createTimeEnd);
        
        List<Tag> tags = tagService.list(Tag.class, filter, page);

        int count = tagService.count(filter);

        // handle query string
        String queryString = request.getQueryString();
        Map<String, String> queries = StringUtils.parseQueryString(queryString);
        if(queries.containsKey("page")) {
            queries.remove("page");
        }

        modelAndView.addObject("tags", tags);
        modelAndView.addObject("count", count);
		modelAndView.addObject("queryString", StringUtils.toQueryString(queries));

        return modelAndView;
    }
    
}
